package pl.com.margielewski.mmacontrols;

import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MMDatePickerDialog extends Dialog implements
		DatePickerDialog.OnDateSetListener, OnClickListener {

	private final Button btnCancel;
	private final Button btnSelect;
	private final String dateFromat = "dd-MM-yyyy";
	private final DatePicker dpDate;
	private Date selectedDate;
	private final TextView tvControlDate;

	public MMDatePickerDialog(Context context, TextView tvControlDate) {
		super(context);
		super.setCancelable(true);

		this.setContentView(R.layout.date_picker_dialog);
		this.setTitle(R.string.litSetDate);

		this.btnSelect = (Button) findViewById(R.id.btnSelect);
		this.btnSelect.setOnClickListener(this);

		this.btnCancel = (Button) findViewById(R.id.btnCancel);
		this.btnCancel.setOnClickListener(this);

		this.dpDate = (DatePicker) findViewById(R.id.dtPicker);
		this.dpDate.setOnClickListener(this);

		this.tvControlDate = tvControlDate;
	}

	public Date getSelectedDate() {
		return this.selectedDate;
	}

	public void onClick(View v) {

		if (v.getId() == R.id.btnSelect) {
			DatePicker tmpPicker = (DatePicker) findViewById(R.id.dtPicker);

			this.selectedDate = new Date(tmpPicker.getYear() - 1900,
					tmpPicker.getMonth(), tmpPicker.getDayOfMonth());

			this.tvControlDate.setText(DateFormat.format(this.dateFromat,
					this.selectedDate));
		}
		this.dismiss();
	}

	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {

		this.selectedDate = new Date(year, monthOfYear, dayOfMonth);
	}
}